package com.letao.action;

import java.util.Date;
import java.util.Map;

import com.letao.data.AppMessage;
import com.letao.pojo.User;
import com.letao.service.UserServiceInterface;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private UserServiceInterface userServiceInterface;
	private User user;
	private int appMessage;

	public String regist() throws Exception {
		if (userServiceInterface.checkUserByUalais(user.getUalais())) {
			// 该用户已经被注册，请重新选择用户名
			ActionContext.getContext().getSession().put("user", user);
			userServiceInterface.addUser(user);
			appMessage = AppMessage.REGIST_SUCCESS;
		} else {
			appMessage = AppMessage.UALAIS_EXIT;
		}
		return INPUT;
	}

	public String validateUalais() throws Exception {
		if (userServiceInterface.checkUserByUalais(user.getUalais())) {
			// 该用户已经被注册，请重新选择用户名
			appMessage = AppMessage.UALAIS_AVAILABLE;
		} else {
			appMessage = AppMessage.UALAIS_EXIT;
		}
		return INPUT;
	}

	public String login() throws Exception {
		User databaseUser=userServiceInterface.checkUserByUalaisAndPass(user.getUalais(),
				user.getUpassword());
		if (databaseUser!=null) {
			ActionContext.getContext().getSession().put("user", databaseUser);
			appMessage = AppMessage.LOGIN_SUCCESS;
		} else {
			appMessage = AppMessage.LOGIN_FAIL;
		}
		return INPUT;
	}

	public String exitlogin() throws Exception {
		ActionContext.getContext().getSession().remove("user");
		appMessage = AppMessage.EXITLOGIN_SUCCESS;
		return INPUT;
	}

	public String getUserInfo() throws Exception {
		user = userServiceInterface.getUserByUid(user.getUid());
		return INPUT;
	}

	public String updateUserInfor() throws Exception {
		userServiceInterface.updateUser(user);
		appMessage = AppMessage.UPDATEUSER_SUCCESS;
		return INPUT;
	}

	public UserServiceInterface getUserServiceInterface() {
		return userServiceInterface;
	}

	public void setUserServiceInterface(
			UserServiceInterface userServiceInterface) {
		this.userServiceInterface = userServiceInterface;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public int getAppMessage() {
		return appMessage;
	}

	public void setAppMessage(int appMessage) {
		this.appMessage = appMessage;
	}

}
